﻿using DG.Tweening;
using System;
using Cinemachine;
using UnityEngine;
using WorldMap;
using Yoozoo.Gameplay.RTS;

namespace UnityGameFramework.Runtime
{
    public sealed partial class DebuggerComponent : GameFrameworkComponent<DebuggerComponent>
    {
        private sealed class SLGCameraWindow : ScrollableDebuggerWindowBase
        {
            private const float TitleWidth = 40f;

            private bool m_isZoom;

            private float ArtLOD0 = 16f;
            
            // private List<LODLevelEnum> m_targetLOD;

            protected override void OnDrawScrollableWindow()
            {
                // GUILayout.Label("<b>Camera Settings</b>");
                // GUILayout.BeginVertical("box");
                // {
                //     if (WorldCameraManager.cameraController != null)
                //     {
                //         //OnDrawHorizontalSlider("高帧率", Application.targetFrameRate >= 59,"Open/Close", ModifyTargetFps);
                //     
                //         //DrawItem("Camera.localPosition", Camera.main.transform.localPosition.ToString());
                //         // OnDrawHorizontalTextField("阻力", WorldCameraManager.cameraController.GetComponent<WorldMapCameraStrategySerializer>().DampFactorTimeMultiplier, ModifyDampFactor);
                //         // OnDrawHorizontalTextField("最近距离", WorldCameraManager.cameraController.ZoomDistanceMin, ModifyZoomMin);
                //         // OnDrawHorizontalTextField("最远距离", WorldCameraManager.cameraController.ZoomDistanceMax, ModifyZoomMax);
                //         // OnDrawHorizontalTextField("缩放速度", WorldCameraManager.cameraController.ZoomSpeed, ModifyZoomSpeed);
                //         // OnDrawHorizontalTextField("缩放速度相关系数", WorldCameraManager.cameraController.GetComponent<WorldMapCameraStrategySerializer>().ZoomFactorDistanceMultiplier, ModifyZoomFactorDistanceMultiplier);
                //          //OnDrawHorizontalTextField("指针相机平移最大时间", WorldMapUtility.ToPosMaxTime, (f =>
                //          //   {
                //          //       WorldMapUtility.ToPosMaxTime = f;
                //          //   }));
                //         //OnDrawHorizontalTextField("FOV", Camera.main.fieldOfView, ModifyFOV);
                //     
                //         // OnDrawHorizontalTextField("城堡最小缩放系数", ref WorldMapCityObject.CITY_SCALE_RATIO_MIN);
                //         // OnDrawHorizontalTextField("城堡最大缩放系数", ref WorldMapCityObject.CITY_SCALE_RATIO_MAX);
                //         // OnDrawHorizontalTextField("其它物件最小缩放系数", ref WorldMapCityObject.OTHER_SCALE_RATIO_MIN);
                //         // OnDrawHorizontalTextField("其它物件最大缩放系数", ref WorldMapCityObject.OTHER_SCALE_RATIO_MAX);
                //     
                //         // OnDrawHorizontalTextField("部队缩放最小值", ref SpriteUnit.ArmyScaleMin);
                //         // OnDrawHorizontalTextField("部队缩放参数", ref SpriteUnit.ArmyScaleValue);
                //     
                //     
                //         // OnDrawHorizontalTextField("LOD1树缩放最大值", ref WorldMapTileElementsObject.LOD1TreeScaleMax);
                //         // OnDrawHorizontalTextField("LOD1树缩放参数", ref WorldMapTileElementsObject.TreeScaleValue);
                //     
                //     
                //       //  DrawCameraDefaultPosition();
                //     
                //       //  DrawItem("注意：", "这里LOD0~LOD5是对应美术的值");
                //         // DrawLOD();
                //         //
                //         // OnDrawHorizontalTextField("LOD0", ref ArtLOD0);
                //         // OnDrawHorizontalTextField("LOD1", ref WorldEntry.DISTANCE_LOD0);
                //         //  OnDrawHorizontalTextField("LOD2", ref WorldEntry.DISTANCE_LOD1);
                //         //  OnDrawHorizontalTextField("LOD3", ref WorldEntry.DISTANCE_LOD2);
                //         //  OnDrawHorizontalTextField("LOD4", ref WorldEntry.DISTANCE_LOD3);
                //         //  OnDrawHorizontalTextField("LOD5", ref WorldEntry.DISTANCE_LOD4);
                //         //  //OnDrawHorizontalTextField("LOD5", ref WorldEntry.DISTANCE_LOD5);
                //     }
                // }
                // GUILayout.EndVertical();
            }

            public void DrawCameraDefaultPosition()
            {
                if(Instance.cameraPosition == null)
                {
                    return;
                }

                GUILayout.BeginHorizontal();
                for (int i = 0; i < Instance.cameraPosition.Count; i++)
                {
                    if (GUILayout.Button("默认视角" + i, GUILayout.Height(60f)))
                    {
                        Camera.main.transform.position = Instance.cameraPosition[i];
                    }
                }
                GUILayout.EndHorizontal();
            }

            public override void OnUpdate(float elapseSeconds, float realElapseSeconds)
            {
                base.OnUpdate(elapseSeconds, realElapseSeconds);
            }

            public void DrawText(string title, string content)
            {
                GUILayout.BeginHorizontal();
                {
                    GUILayout.Label(title, GUILayout.Width(TitleWidth));
                    GUILayout.Label(content);
                }
                GUILayout.EndHorizontal();
            }

            public void DrawLOD()
            {
                GUILayout.BeginHorizontal();
                for (int i = 0; i < 6; i++)
                {
                    if (GUILayout.Button("跳转LOD" + i, GUILayout.Height(60f)))
                    {
                        LODLevelEnum lodLevel = (LODLevelEnum)(i);
                        float coefficient = 1.414f + 0.002f;
                        switch (lodLevel)
                        {
                             case LODLevelEnum.LOD0:
                                 WorldCameraManager.ZoomTo(ArtLOD0 * coefficient, 0.5f, Ease.InOutQuad);
                                 break;
                             case LODLevelEnum.LOD1:
                                 WorldCameraManager.ZoomTo(WorldEntry.DISTANCE_LOD0 * coefficient, 0.5f, Ease.InOutQuad);
                                 break;
                             case LODLevelEnum.LOD2:
                                 WorldCameraManager.ZoomTo(WorldEntry.DISTANCE_LOD1 * coefficient, 0.5f, Ease.InOutQuad);
                                 break;
                             case LODLevelEnum.LOD3:
                                 WorldCameraManager.ZoomTo(WorldEntry.DISTANCE_LOD2 * coefficient, 0.5f, Ease.InOutQuad);
                                 break;
                             case LODLevelEnum.LOD4:
                                 WorldCameraManager.ZoomTo(WorldEntry.DISTANCE_LOD3 * coefficient, 0.5f, Ease.InOutQuad);
                                 break;
                             case LODLevelEnum.LOD5:
                                 WorldCameraManager.ZoomTo(WorldEntry.DISTANCE_LOD4 * coefficient, 0.5f, Ease.InOutQuad);
                                 break;
                        }
                    }
                }
                GUILayout.EndHorizontal();
            }

            public void ModifyFOV(float value)
            {
               // CameraManager.cameraController.GetComponent<CinemachineVirtualCamera>().m_Lens.FieldOfView = value;
            }

            public void ModifyDampFactor(float value)
            {
              //  CameraManager.cameraController.GetComponent<WorldMapCameraStrategySerializer>().DampFactorTimeMultiplier = value;
            }

            public void ModifyZoomSpeed(float value)
            {
               // CameraManager.cameraController.ZoomSpeed = value;
            }

            public void ModifyZoomFactorDistanceMultiplier(float value)
            {
              //  CameraManager.cameraController.GetComponent<WorldMapCameraStrategySerializer>().ZoomFactorDistanceMultiplier = value;
            }

            public void ModifyTargetFps(bool highFps)
            {
                Application.targetFrameRate = (highFps ? 60 : 30);
            }

            public void ModifyZoomMin(float value)
            {
              //  CameraManager.cameraController.ZoomDistanceMin = value;
            }

            public void ModifyZoomMax(float value)
            {
               // CameraManager.cameraController.ZoomDistanceMax = value;
            }

            private void OnDrawHorizontalSlider(string label, ref float value, float min, float max, Action<float> modifyCallback)
            {
                GUILayout.BeginHorizontal();
                {
                    float temp = value;
                    label = string.Format("{0}:{1}", label, value);
                    GUILayout.Label(label, GUILayout.Width(120f));
                    if (GUILayout.RepeatButton("-", GUILayout.Width(30f)))
                    {
                        temp--;
                    }
                    temp = GUILayout.HorizontalSlider(temp, min, max);
                    if (GUILayout.RepeatButton("+", GUILayout.Width(30f)))
                    {
                        temp++;
                    }
                    temp = Mathf.Clamp(temp, min, max);
                    if (temp != value)
                    {
                        temp = (float)Math.Round(temp,2);
                        if (modifyCallback!=null)
                        {
                            modifyCallback(temp);
                        }

                        value = temp;
                    }
                }
                GUILayout.EndHorizontal();
            }

            private void OnDrawHorizontalSlider(string label, float value, float min, float max, Action<float> modifyCallback)
            {
                OnDrawHorizontalSlider(label,ref value,min,max, modifyCallback);
            }

            private void OnDrawHorizontalTextField(string label, float value, Action<float> modifyCallback = null)
            {
                OnDrawHorizontalTextField(label, ref value, modifyCallback);
            }

            private void OnDrawHorizontalTextField(string label,ref float value, Action<float> modifyCallback = null)
            {
                GUILayout.BeginHorizontal();
                {
                    float temp = value;
                    GUILayout.Label(label, GUILayout.Width(120f));
                    string tempStr = GUILayout.TextField(Convert.ToString(value));
                    tempStr = string.IsNullOrEmpty(tempStr) ? "0" : tempStr;
                    temp = (float)Convert.ToDouble(tempStr);
                    if (temp != value)
                    {
                        if (modifyCallback != null)
                        {
                            modifyCallback(temp);
                        }

                        value = temp;
                    }
                }
                GUILayout.EndHorizontal();
            }

            private void OnDrawHorizontalToggle(string label, bool value, string toogleStr, Action<bool>
                 modifyCallback = null)
            {
                GUILayout.BeginHorizontal();
                {
                    bool temp = value;
                    label = string.Format("{0}:{1}", label, value);
                    GUILayout.Label(label, GUILayout.Width(120f));
                    temp = GUILayout.Toggle(value, toogleStr, GUILayout.Width(120f));
                    if (temp != value)
                    {
                        if (modifyCallback != null)
                        {
                            modifyCallback(temp);
                        }

                        value = temp;
                    }
                }
                GUILayout.EndHorizontal();
            }
        }
    }
}
